<?php
  /**
   * @author 2mdc.com
   * @version 
   */

  include_once dirname(__FILE__) . '/cCreateElement.inc';

  class cCreateTable extends cCreateElement {
    static private $rscInstance = NULL;

    public function __construct() {
    }

    public function __destruct() {
    }

    public function __toString() {
      $this->fCleanTemplate();
      return $this->xml;
    }

    static public function getInstance() {
       if (self::$rscInstance == NULL) {
          self::$rscInstance = new cCreateTable();
       }
       return self::$rscInstance;
    }

    private function fCleanTemplateR() {
      $this->xml = preg_replace('/__GENERATETR__/', '', $this->xml);
    }

    protected function fAddList($strList) {
      $this->xml = str_replace('__GENERATEP__', $strList, $this->xml);
    }

    protected function fGenerateGRIDCOLS($w) {
      $this->xml = str_replace('__GENERATEGRIDCOLS__', '<'.cCreateElement::NAMESPACE.':tblGrid '.cCreateElement::NAMESPACE.':w="'.$w.'"></'.cCreateElement::NAMESPACE.':tblGrid>__GENERATEGRIDCOLS__', $this->xml);
    }

    protected function fGenerateHMERGE($val = '') {
    }

    protected function fGenerateJC($strVal = '') {
      $this->xml = str_replace('__GENERATEJC__', '<'.cCreateElement::NAMESPACE.':jc '.cCreateElement::NAMESPACE.':val="'.$strVal.'"></'.cCreateElement::NAMESPACE.':jc>', $this->xml);
    }

    protected function fGenerateP() {
      $this->xml = str_replace('__GENERATETC__', '<'.cCreateElement::NAMESPACE.':p >__GENERATEP__</'.cCreateElement::NAMESPACE.':p>', $this->xml);
    }

    protected function fGenerateSHD($val="horz-cross", $color="ffff00", $fill="", $bgcolor="") {
      $xmlAux = '<'.cCreateElement::NAMESPACE.':shd val="'.$val.'"';
      if($color != '')
      	$xmlAux .= ' color="'.$color.'"';
      if($fill != '')
      	$xmlAux .= ' fill="'.$fill.'"';
      if($bgcolor != '')
      	$xmlAux .= ' wx:bgcolor="'.$bgcolor.'"';
      $xmlAux .= '></'.cCreateElement::NAMESPACE.':shd>';      
      $this->xml = str_replace('__GENERATESHD__', $xmlAux, $this->xml);
    }

    protected function fGenerateTBLBORDERS() {
      $this->xml = str_replace('__GENERATETBLBORDERS__', '<'.cCreateElement::NAMESPACE.':tblBorders>__GENERATETBLBORDER__</'.cCreateElement::NAMESPACE.':tblBorders>', $this->xml);
    }
  
    protected function fGenerateTBLBOTTOM($val="single", $sz="4", $space='0', $color='auto') {
      $this->xml = str_replace('__GENERATETBLBORDER__', '<'.cCreateElement::NAMESPACE.':bottom '.cCreateElement::NAMESPACE.':val="'.$val.'" '.cCreateElement::NAMESPACE.':sz="'.$sz.'" '.cCreateElement::NAMESPACE.':space="'.$space.'" '.cCreateElement::NAMESPACE.':color="'.$color.'"></'.cCreateElement::NAMESPACE.':bottom>__GENERATETBLBORDER__', $this->xml);
    }
  
    protected function fGenerateTBL() {
      $this->xml = '<'.cCreateElement::NAMESPACE.':tbl>__GENERATETBL__</'.cCreateElement::NAMESPACE.':tbl>';
    }

    protected function fGenerateTBLSTYLE($strVal = 'TableGrid') {
      $this->xml = str_replace('__GENERATETBLSTYLE__', '<'.cCreateElement::NAMESPACE.':tblStyle '.cCreateElement::NAMESPACE.':val="'.$strVal.'"></'.cCreateElement::NAMESPACE.':tblStyle>', $this->xml);
    }
    
    protected function fGenerateTBLCELLSPACING($w = '', $type = '') {
      $this->xml = str_replace('__GENERATETBLCELLSPACING__', '<'.cCreateElement::NAMESPACE.':tblCellSpacing '.cCreateElement::NAMESPACE.':w="'.$w.'" '.cCreateElement::NAMESPACE.':type="'.$type.'"></'.cCreateElement::NAMESPACE.':tblCellSpacing>', $this->xml);
    }

    protected function fGenerateTBLGRID() {
      $this->xml = str_replace('__GENERATETBL__', '<'.cCreateElement::NAMESPACE.':tblGrid>__GENERATEGRIDCOLS__</'.cCreateElement::NAMESPACE.':tblGrid>__GENERATETBL__', $this->xml);
    }

    protected function fGenerateTBLINSIDEH($val="single", $sz="4", $space='0', $color='auto') {
      $this->xml = str_replace('__GENERATETBLBORDER__', '<'.cCreateElement::NAMESPACE.':insideH '.cCreateElement::NAMESPACE.':val="'.$val.'" '.cCreateElement::NAMESPACE.':sz="'.$sz.'" '.cCreateElement::NAMESPACE.':space="'.$space.'" '.cCreateElement::NAMESPACE.':color="'.$color.'"></'.cCreateElement::NAMESPACE.':insideH>__GENERATETBLBORDER__', $this->xml); 
    }
  
    protected function fGenerateTBLINSIDEV($val="single", $sz="4", $space='0', $color='auto') {
      $this->xml = str_replace('__GENERATETBLBORDER__', '<'.cCreateElement::NAMESPACE.':insideV '.cCreateElement::NAMESPACE.':val="'.$val.'" '.cCreateElement::NAMESPACE.':sz="'.$sz.'" '.cCreateElement::NAMESPACE.':space="'.$space.'" '.cCreateElement::NAMESPACE.':color="'.$color.'"></'.cCreateElement::NAMESPACE.':insideV>__GENERATETBLBORDER__', $this->xml);
    }

    protected function fGenerateTBLLEFT($val="single", $sz="4", $space='0', $color='auto') {
      $this->xml = str_replace('__GENERATETBLBORDER__', '<'.cCreateElement::NAMESPACE.':left '.cCreateElement::NAMESPACE.':val="'.$val.'" '.cCreateElement::NAMESPACE.':sz="'.$sz.'" '.cCreateElement::NAMESPACE.':space="'.$space.'" '.cCreateElement::NAMESPACE.':color="'.$color.'"></'.cCreateElement::NAMESPACE.':left>__GENERATETBLBORDER__', $this->xml);
    }
  
    protected function fGenerateTBLLOOK($strVal = '000001E0') {
      $this->xml = str_replace('__GENERATETBLLOOK__', '<'.cCreateElement::NAMESPACE.':tblLook '.cCreateElement::NAMESPACE.':val="'.$strVal.'"></'.cCreateElement::NAMESPACE.':tblLook>', $this->xml);
    }

    protected function fGenerateTBLOVERLAP($strVal = 'never') {
      $this->xml = str_replace('__GENERATETBLOVERLAP__', '<'.cCreateElement::NAMESPACE.':tblOverlap '.cCreateElement::NAMESPACE.':val="'.$strVal.'"></'.cCreateElement::NAMESPACE.':tblOverlap>', $this->xml);
    }

    protected function fGenerateTBLPR() {
      $this->xml = str_replace('__GENERATETBL__', '<'.cCreateElement::NAMESPACE.':tblPr>__GENERATETBLSTYLE____GENERATEJC____GENERATETBLW____GENERATETBLLOOK____GENERATETBLOVERLAP____GENERATETBLCELLSPACING____GENERATETBLBORDERS____GENERATESHD__</'.cCreateElement::NAMESPACE.':tblPr>__GENERATETBL__', $this->xml);
    }

    protected function fGenerateTBLRIGHT($val="single", $sz="4", $space='0', $color='auto') {
      $this->xml = str_replace('__GENERATETBLBORDER__', '<'.cCreateElement::NAMESPACE.':right '.cCreateElement::NAMESPACE.':val="'.$val.'" '.cCreateElement::NAMESPACE.':sz="'.$sz.'" '.cCreateElement::NAMESPACE.':space="'.$space.'" '.cCreateElement::NAMESPACE.':color="'.$color.'"></'.cCreateElement::NAMESPACE.':right>__GENERATETBLBORDER__', $this->xml);
    }

    protected function fGenerateTBLTOP($val="single", $sz="4", $space='0', $color='auto') {
      $this->xml = str_replace('__GENERATETBLBORDER__', '<'.cCreateElement::NAMESPACE.':top '.cCreateElement::NAMESPACE.':val="'.$val.'" '.cCreateElement::NAMESPACE.':sz="'.$sz.'" '.cCreateElement::NAMESPACE.':space="'.$space.'" '.cCreateElement::NAMESPACE.':color="'.$color.'"></'.cCreateElement::NAMESPACE.':top>__GENERATETBLBORDER__', $this->xml);
    }

    protected function fGenerateTBLW($strType = 'auto', $strW = '0') {
      $this->xml = str_replace('__GENERATETBLW__', '<'.cCreateElement::NAMESPACE.':tblW '.cCreateElement::NAMESPACE.':w="'.$strW.'" '.cCreateElement::NAMESPACE.':type="'.$strType.'"></'.cCreateElement::NAMESPACE.':tblW>', $this->xml);
    }

    protected function fGenerateTC() {
      $this->xml = str_replace('__GENERATETR__', '<'.cCreateElement::NAMESPACE.':tc >__GENERATETC__</'.cCreateElement::NAMESPACE.':tc>__GENERATETR__', $this->xml);
    }

    protected function fGenerateTCPR() {
    	$this->xml = str_replace('__GENERATETC__', '<'.cCreateElement::NAMESPACE.':tcPr>__GENERATETCPR__</'.cCreateElement::NAMESPACE.':tcPr>__GENERATETC__', $this->xml);
    }

    protected function fGenerateTCW($w = '', $type = 'dxa') {
      $this->xml = str_replace('__GENERATETCPR__', '<'.cCreateElement::NAMESPACE.':tcW '.cCreateElement::NAMESPACE.':w="'.$w.'" '.cCreateElement::NAMESPACE.':type="'.$type.'"></'.cCreateElement::NAMESPACE.':tcW>', $this->xml);
    }

    protected function fGenerateTR() {
      $this->xml = str_replace('__GENERATETBL__', '<'.cCreateElement::NAMESPACE.':tr >__GENERATETR__</'.cCreateElement::NAMESPACE.':tr>__GENERATETBL__', $this->xml);
    }
    
    protected function fGenerateTRPR() {
    }

    protected function fGenerateVMERGE($val = '') {
    }

    public function fCreateTable() {
      $this->xml = '';
      $arrArgs = func_get_args();

      if(is_array($arrArgs[0])) {
        $this->fGenerateTBL();
        $this->fGenerateTBLPR();
        $this->fGenerateTBLSTYLE();
        $this->fGenerateTBLW();
        if(!empty($arrArgs[1]['border'])){
        	$this->fGenerateTBLBORDERS();
        	$this->fGenerateTBLBOTTOM();
	        $this->fGenerateTBLLEFT();
	        $this->fGenerateTBLTOP();
	        $this->fGenerateTBLRIGHT();
	        $this->fGenerateTBLINSIDEH();
	        $this->fGenerateTBLINSIDEV();
        }
        $this->fGenerateTBLLOOK();
        $this->fGenerateTBLOVERLAP();
        $intLine = 0;
        foreach($arrArgs[0] as $arrDatDepth) {
          $this->fGenerateTR();
          if($intLine == 0) {
              $this->fGenerateTC();
              $this->fGenerateP();
              $this->fGenerateR();
              $this->fGenerateT('');
          }
          $intLine++;
          foreach($arrDatDepth as $unkDat) {
            $this->fGenerateTC();
            $this->fGenerateP();
            $this->fGenerateR();            
            $this->fGenerateT($unkDat);
          }
          $this->fCleanTemplateR();
        }
      }
    }
  }
?>